package com.hmall.api.config;

import com.hmall.api.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }
    @Bean
    public CartClientFallback cartClientFallback(){
        return new CartClientFallback();
    }
    @Bean
    public TradeClientFallback tradeClientFallback(){
        return new TradeClientFallback();
    }
    @Bean
    public UserClientFallback userClientFallback(){
        return new UserClientFallback();
    }
    //配置feign的日志级别

    @Bean
    public System.Logger.Level feignLoggerLevel(){
        return System.Logger.Level.ALL;
    }
    //定义feign请求拦截器，设置用户信息
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if (userId != null){
                    requestTemplate.header("user-info",userId.toString());
                }
            }
        };
    }

    @Bean
    public PayClientFallback payClientFallback(){
        return new PayClientFallback();
    }

}
